package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.LoginFailedException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageBean;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.swing.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 登录检查
     */
    @Override
    public EmployeeLoginVO checkLogin(EmployeeLoginDTO employeeLoginDTO) {
        Employee username = employeeMapper.checkUsernname(employeeLoginDTO.getUsername());
        if (username == null) {
            throw new LoginFailedException("账户错误！");
        }
        Employee passwprd = employeeMapper.checkPassword(employeeLoginDTO.getPassword());
        if (passwprd == null) {
            throw new LoginFailedException("密码错误！");
        }
        //定义用于存放放入jwt中的信息
        Map<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.EMP_ID, username.getId());
        map.put(JwtClaimsConstant.USER_ID, username.getIdNumber());
        map.put(JwtClaimsConstant.PHONE, username.getPhone());
        map.put(JwtClaimsConstant.USERNAME, username.getUsername());
        map.put(JwtClaimsConstant.NAME, username.getName());

        //创建JWT
        String jwt = JwtUtil.createJWT("itcast", 600000000, map);

        //返回数据
        return new EmployeeLoginVO(username.getId(), username.getUsername(), username.getName(), jwt);

    }

    /**
     * 新增员工功能
     *
     * @param employeeDTO
     */
    @Override
    public void addEmp(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);//克隆对象
        employee.setStatus(StatusConstant.ENABLE);//通过长亮设置状态
        //设置默认密码，一定要加密！通过DU工具类
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //设置创建时间和更新时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置创建人和修改人id 4 TODO 没有写创建人的更新人id
        employee.setCreateUser(10L);
        employee.setUpdateUser(10L);


        employeeMapper.insert(employee);


    }

    /**
     * 分页查询功能
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageBean pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {

        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> page= employeeMapper.pageQuery(employeePageQueryDTO);
        return new PageBean(page.getTotal(),page.getResult());

    }

    /**
     * 更改员工的状态
     *
     * @param status
     * @param id
     */
    @Override
    public void setStatus(Integer status, Long id) {
        Employee employee=new Employee();
        employee.setStatus(status);
        employee.setId(id);
        employeeMapper.updateEmp(employee);
    }

    /**
     * 根据id查找员工
     *
     * @param id
     * @return
     */
    @Override
    public Employee getById(Integer id) {
        Employee employee= employeeMapper.getById(id);
        employee.setPassword("******");
        return employee;
    }

    /**
     * @param employeeDTO
     */
    @Override
    public void updateById(EmployeeDTO employeeDTO) {
        Employee employee=new Employee();

        BeanUtils.copyProperties(employeeDTO, employee);
        //设置更新人和更新时间
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(BaseContext.getCurrentId());

        employeeMapper.updateEmp(employee);
    }
}
